package com.code.soulnetty.modbus.client.service;

import lombok.Getter;

/**
 * modbus 功能码
 * 寄存器分类
 * <li>线圈寄存器:plc(00001-09999)</li>
 * <li>离散输入寄存器:plc(10001-19999)</li>
 * <li>输入寄存器:plc(30001-39999)</li>
 * <li>保持寄存器:plc(40001-49999)</li>
 *
 * @author jdy
 * @date 3:58 下午
 **/
@Getter
public enum FunctionCode {

    /**
     * 读取线圈寄存器，位操作
     */
    READ_COILS(1, "读取线圈"),
    /**
     * 读取离散寄存器,位操作
     */
    READ_DISCRETE_INPUTS(2, "读取离散寄存器"),
    /**
     * 读取保持寄存器,字操作
     */
    READ_HOLDING_REGISTERS(3, "读取保持寄存器"),
    /**
     * 读取输入寄存器,字操作
     */
    READ_INPUT_REGISTERS(4, "读取输入寄存器"),
    /**
     * 写单个线圈,位操作
     */
    WRITE_COIL(5, "写单个线圈"),
    /**
     * 写单个保持寄存器，字操作
     */
    WRITE_REGISTER(6, "写单个寄存器"),
    /**
     * 写多个线圈，位操作
     */
    WRITE_COILS(15, "写多个线圈"),
    /**
     * 写多个保持寄存器，字操作
     */
    WRITE_REGISTERS(16, "写多个寄存器");

    /**
     * 功能码
     */
    private int fc;
    /**
     * 描述
     */
    private String desc;

    FunctionCode(int fc, String desc) {
        this.fc = fc;
        this.desc = desc;
    }

    public static boolean isRead(int fc) {
        return fc >= 1 && fc <= 4;
    }

    public static boolean isWrite(int fc) {
        return fc == WRITE_COIL.fc || fc == WRITE_COILS.fc || fc == WRITE_REGISTER.fc || fc == WRITE_REGISTERS.fc;
    }

    public byte fc() {
        return (byte) fc;
    }
}
